package com.shou.zhnfyxt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shou.zhnfyxt.entity.HouseDetail;
import com.shou.zhnfyxt.entity.HouseRecommendation;
import com.shou.zhnfyxt.entity.R;
import com.shou.zhnfyxt.mapper.HouseRecommendationDao;
import com.shou.zhnfyxt.service.HouseDetailService;
import com.shou.zhnfyxt.service.IHouseRecommendationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author JCY
 * @since 2022-12-30
 */
@RestController
@RequestMapping("/houseRecommendation")
public class HouseRecommendationController {
    @Autowired
    private HouseRecommendationDao houseRecommendationDao;
    @Autowired
    private IHouseRecommendationService houseRecommendationService;

    @Autowired
    private HouseDetailService houseDetailService;

    @PostMapping("click")
    public R<Integer> click(@RequestBody Integer houseId) {
        QueryWrapper<HouseRecommendation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("house_id", houseId);
        HouseRecommendation houseRecommendation = houseRecommendationDao.selectOne(queryWrapper);
        if (houseRecommendation == null) {
            HouseRecommendation newHouseRecommendation = new HouseRecommendation();
            newHouseRecommendation.setHouseId(houseId);
            newHouseRecommendation.setHouseType(2);
            newHouseRecommendation.setHouseClick(1);
            houseRecommendationService.save(newHouseRecommendation);
        } else {
            Integer houseClick = houseRecommendation.getHouseClick();
            houseClick++;
            houseRecommendation.setHouseClick(houseClick);
            houseRecommendationService.update(houseRecommendation, queryWrapper);
        }
        return R.success(200);
    }

    @PostMapping("getHotClick")
    public R<List<HouseDetail>> getHotClick() {
        return houseRecommendationService.getHotClick();
    }
}
